package com.lianxi.stu;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author xiyu
 * @version 1.0
 * @date 2025/4/1 15:04
 * @description
 */
public class StudentDao {
    Scanner input = new Scanner(System.in);
    ArrayList<Student> students = new ArrayList<>();
    public void addStu(){
        System.out.print("请输入要添加的学生姓名：");
        String name = input.next();
        System.out.print("请输入要添加的学生学号：");
        int sid = input.nextInt();
        System.out.print("请输入要添加的学生年龄：");
        int age = input.nextInt();
        try {
            FileOutputStream fos = new FileOutputStream("D:\\course\\01-JavaSE\\20250407IO流\\students\\student");
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            Student student = new Student(name, sid, age);
            oos.writeObject(student);
            oos.close();
        } catch (FileNotFoundException e) {
//            e.printStackTrace();
        } catch (IOException e) {
//            e.printStackTrace();
        }

        System.out.println("========添加完成========");
    }

    public void findInfobyName(){
        System.out.print("请输入要查询的学生姓名：");
        String name = input.next();
        boolean flag = false;
        for (Student student : students) {
            if (student.getName().equals(name)){
                System.out.println(student);
                flag = true;
            }
        }
        if (flag){
            System.out.println("=======查询完成=====");
        }else {
            System.out.println("=======查无此人=====");
        }
    }

    public void editInfobyName(){
        System.out.print("请输入要修改的学生姓名：");
        String name = input.next();
        boolean flag = false;
        for (Student student : students) {
            if (student.getName().equals(name)){
                System.out.print("请输入修改后的学生姓名：");
                student.setName(input.next());
                System.out.print("请输入修改后的学生学号：");
                int edsid = input.nextInt();
                for (Student student1 : students) {
                    int sid1 = student1.getSid();
                    if (edsid==sid1){
                        System.err.println("学号重复");
                        return;
                    }
                }
                student.setSid(edsid);
                System.out.print("请输入修改后的学生年龄：");
                student.setAge(input.nextInt());
                flag = true;
            }
        }
        if (flag){
            System.out.println("=======修改完成=====");
        }else {
            System.out.println("=======查无此人=====");
        }
    }

    public void delStubyName(){
        System.out.print("请输入要删除的学生姓名：");
        String name = input.next();
        boolean flag = false;
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getName().equals(name)){
                students.remove(i);
                flag = true;
            }
        }
        if (flag){
            System.out.println("=======删除完成=====");
        }else {
            System.out.println("=======查无此人=====");
        }
    }

    public void memu(){
        while (true){
            System.out.println("==学生信息管理系统==");
            System.out.println("1.添加学生");
            System.out.println("2.修改学生信息");
            System.out.println("3.删除学生信息");
            System.out.println("4.查询学生信息");
            System.out.println("5.退出系统");
            System.out.print("请输入您的需求：");
            int next = input.nextInt();
            switch (next){
                case 1:
                    addStu();
                    break;
                case 2:
                    editInfobyName();
                    break;
                case 3:
                    delStubyName();
                    break;
                case 4:
                    findInfobyName();
                    break;
                default:
                    return;

            }
        }
    }
    {
        students.add(new Student("李大",1,18));
        students.add(new Student("李二",2,19));
        students.add(new Student("李三",3,17));
    }
}
